package whf.framework.dao.hibernate;

import java.util.Calendar;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.orm.hibernate3.HibernateTemplate;

import whf.framework.dao.param.IParam;
import whf.framework.log.Log;
import whf.framework.log.LogFactory;
import whf.framework.util.BeanFactory;
import whf.framework.util.BeanInitialization;

/**
 * @author wanghaifeng
 *
 */
public class HibernateHelper {
	private static Log log = LogFactory.getLog(HibernateHelper.class);
	
	public static List getNamedQueryResult(String queryName, List<IParam> params){
		return getNamedQueryResult(queryName, params, 0, Integer.MAX_VALUE);
	}
	
	/**
	 * 获取命名查询
	 * @modify wanghaifeng Dec 28, 2006 11:15:52 AM
	 * @param queryName
	 * @param params
	 * @param start
	 * @param pageSize
	 * @return
	 */
	public static List getNamedQueryResult(String queryName, List<IParam> params, int start, int pageSize){
		Session session = null;
		try{
			SessionFactory sf = BeanFactory.getSessionFactory();
			session = sf.openSession();
			Query query = session.getNamedQuery(queryName);
			log.debug(query.getQueryString());
			log.debug(params);
			for(IParam param: params){
				if(param.getType() == int.class || param.getType() == long.class || param.getType() == Integer.class || param.getType() == Long.class){
					long value = 0;
					if(param.getValue() != null){
						value = ((Long)param.getValue()).longValue();
					}
					query.setLong(param.getName(), value);
				} else if(param.getType() == String.class){
					String value = (String)param.getValue();
					query.setString(param.getName(), value);
				} else if(param.getType() == float.class || param.getType() == double.class || param.getType() == Float.class || param.getType() == Double.class){
					double v = 0;
					if(param.getValue() != null){
						v = ((Double)param.getValue()).doubleValue();
					}
					query.setDouble(param.getName(), v);
				} else if(param.getType() == java.util.Date.class || param.getType() == java.sql.Date.class){
					java.util.Date value = (java.util.Date)param.getValue(); 
					query.setDate(param.getName(), value);
				} else if(param.getType() == Calendar.class){
					query.setCalendar(param.getName(), ((Calendar)param.getValue()));
				} else if(param.getType() == boolean.class){
					query.setBoolean(param.getName(), ((Boolean)param.getValue()).booleanValue());
				} else {
					query.setParameter(param.getName(), param.getValue());
				}
			}
			List list = query.list();
			for(Object o:list){
				BeanInitialization.initializeBean(o);
			}
			return list;
		} catch(RuntimeException e){
			log.error("getNamedQueryResult error: query name is '" + queryName + "'", e);
			throw e;
		}finally{
			session.close();
		}
	}
	
	/**
	 * 获取Spring下的HibernateTemplate
	 * @modify wanghaifeng Dec 28, 2006 11:16:08 AM
	 * @return
	 */
	public static HibernateTemplate getHibernateTemplate(){
		return new HibernateTemplate(BeanFactory.getSessionFactory());
	}
}
